package dao

import (
	"ruoyi-hello/app/sys/model"
	"ruoyi-hello/config"
)

var TableSysMenuTable = "sys_menu"
var TableSysRoleMenuTable = "sys_role_menu"

func SelectMenuPermsByRoleId(role int) []string {

	var sysRoleMenus []model.SysRoleMenu
	err := config.MysqlConn.Primary.Table(TableSysRoleMenuTable).Where(" role_id = ?", role).Find(&sysRoleMenus)
	if err.Error != nil || len(sysRoleMenus) <= 0 {
		return nil
	}
	menuIds := make([]int, len(sysRoleMenus))
	for _, roleMenu := range sysRoleMenus {
		menuIds = append(menuIds, roleMenu.MenuId)
	}
	var sysMenus []model.SysMenu
	err = config.MysqlConn.Primary.Table(TableSysMenuTable).Where(" role_id in ?", menuIds).Find(&sysMenus)
	if err.Error != nil || len(sysMenus) <= 0 {
		return nil
	}
	rolePerms := make([]string, len(sysMenus))
	for _, menu := range sysMenus {
		rolePerms = append(rolePerms, menu.Perms)
	}
	return rolePerms
}
